package com.shengqin.config.rights;

import cn.dev33.satoken.stp.StpInterface;
import com.shengqin.entity.Menu;
import com.shengqin.entity.RoleMenu;
import com.shengqin.entity.User;
import com.shengqin.service.IMenuService;
import com.shengqin.service.IRoleMenuService;
import com.shengqin.service.IUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private IUserService userService;
    @Resource
    private IRoleMenuService roleMenuService;

    @Resource
    private IMenuService menuService;


    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        User user = userService.getById(loginId.toString());
        List<RoleMenu> roleMenus = roleMenuService.listByRoleId(user.getRoleId());
        List<Menu> menus = menuService.listByIds(roleMenus.stream().map(RoleMenu::getMenuId).collect(Collectors.toSet()));
        return menus.stream().filter(item -> item.getType().equals(3)).map(Menu::getCode).distinct().collect(Collectors.toList());
    }


    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> list = new ArrayList<String>();
        User user = userService.getById(loginId.toString());
        list.add(user.getRoleId().toString());
        return list;
    }

}